package com.ibeeking.found.open.b.rest.service.impl;

import com.ibeeking.found.common.config.feign.GlobalClient;
import com.ibeeking.found.common.entity.DictDTO;
import com.ibeeking.found.common.enums.DictTypeEnum;
import com.ibeeking.found.common.enums.qr.QrSceneEnum;
import com.ibeeking.found.open.api.common.param.AppletsQrCodeParam;
import com.ibeeking.found.open.api.feign.SystemFeignClient;
import com.ibeeking.found.open.b.rest.service.IQrService;
import com.ibeeking.found.open.b.rest.service.ISystemService;
import com.ibeeking.found.open.service.common.dto.QrSceneDTO;
import com.ibeeking.found.open.service.common.param.QrCreateParam;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.result.ResponseCodeEnum;
import com.ibeeking.nematos.utils.result.ResponseResult;
import feign.Response;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName QrServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-08-03 13:35
 **/
@Service
public class QrServiceImpl implements IQrService {

    @Resource
    private GlobalClient globalClient;
    @Resource
    private SystemFeignClient systemFeignClient;
    @Resource
    private ISystemService systemServiceImpl;

    @Override
    public List<QrSceneDTO> scenes() {
        //QrSceneEnum
        ResponseResult<List<DictDTO>> responseResult = globalClient.queryDictByType(DictTypeEnum.QR_SCENE.name());
        if (!ResponseCodeEnum.SUCCESS.getCode().equals(responseResult.getCode())) {
            return Collections.emptyList();
        }
        List<QrSceneDTO> result = new ArrayList<>();
        responseResult.getData().forEach(s -> {
            QrSceneDTO qrSceneDTO = new QrSceneDTO();
            qrSceneDTO.setCode(s.getValue());
            qrSceneDTO.setName(s.getLabel());
            result.add(qrSceneDTO);
        });
        return result;
    }

    @Override
    public void createQr(QrCreateParam param, HttpServletResponse response) {
        try {
            AppletsQrCodeParam param1 = new AppletsQrCodeParam();
            //param1.setPath(param.getSceneCode().getPages());
            param1.setPath("pages/main/user/user");
            param1.setQrScene(param.getSceneCode().getCode());
            if (QrSceneEnum.SHOP_PARKING_ISSUE_COUPON.name().equals(param.getSceneCode().name())) {
                //停车场小程序
                param1.setQuery("id=" + systemServiceImpl.queryShopIdByShopCode(param.getSource()));
                param1.setExpireTime(300L);
            }
            Response response1 = systemFeignClient.createSceneQR(param1);
            if (null != response1) {
                Response.Body body = response1.body();
                MemberServiceImpl.stream(body.asInputStream(), response.getOutputStream());
            }
        } catch (IOException e) {
            throw new BusinessException("会员码刷新失败，请重试");
        }
    }
}
